
import { Service } from 'egg';

export default class Sort extends Service {
    /**
    * 获取电影分类类型
    * */
    public async getVideoSort() {
        const { ctx, app } = this;
        const { Joi } = app;

        let schema = Joi.object({
            user: Joi.string().required(),
            type: Joi.string().required(),
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let data = (await app.mysql.get('Yusup').select('settings', {
            where: {
                user: ctx.query.user, setting_name: ctx.query.type == 'tv' ? 'tvSort' : 'videoSort'/* , nation, sort */
            }
        }))[0]
        return {
            code: 200,
            data
        }
    }
    /**
    * 获取电影分类类型
    * */
    public async saveVideoSort() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            setting_name: Joi.string().required(),
            user: Joi.string().required(),
            setting_content: Joi.string().required()
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        // update by primary key ID, and refresh
        let { setting_name, user, setting_content } = ctx.request.body
        /* const row = {
            setting_content
        }; */
        // console.log(ctx.request.body);

        let res = await app.mysql.get('Yusup').query(`update settings set setting_content = '${setting_content}' where setting_name = ? AND user = ?`, [setting_name, user]) //await app.mysql.update('settings', row);
        return {
            code: 200,
            data: res
        }
    }
    /**
    * 获取电影分类类型
    * */
    public async getVideoRegions() {
        const { ctx, app } = this;
        const { Joi } = app;

        let schema = Joi.object({
            user: Joi.string().required(),
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let data = (await app.mysql.get('Yusup').select('settings', {
            where: {
                user: ctx.query.user, setting_name: 'regionList'
            }
        }))[0]
        return {
            code: 200,
            data
        }
    }
    /**
    * 获取电影分类类型
    * */
    public async saveVideoRegions() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            setting_name: Joi.string().required(),
            user: Joi.string().required(),
            setting_content: Joi.string().required()
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        // update by primary key ID, and refresh
        let { setting_name, user, setting_content } = ctx.request.body
        /* const row = {
            setting_content
        }; */
        // console.log(ctx.request.body);

        let res = await app.mysql.get('Yusup').query(`update settings set setting_content = '${setting_content}' where setting_name = ? AND user = ?`, [setting_name, user]) //await app.mysql.update('settings', row);
        return {
            code: 200,
            data: res
        }
    }
}